package com.xjh.utils.file;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.ParseException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import technology.tabula.CommandLineApp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:XuJianHua
 * @Date:2023/1/30
 * @Description:
 */
public class PDFUtil {
    public static void main(String[] args) throws ParseException {
        readPDFDetail();

    }
    public static List<MultipartFile> readPDFDetail() throws ParseException {
        String[] argsa = new String[]{"-f=JSON", "-p=1", "C:\\Users\\25036\\IdeaProjects\\emis\\output\\test(3).pdf", "-l"};
        CommandLineParser parser = new DefaultParser();
        CommandLine cmd = null;
        cmd = parser.parse(CommandLineApp.buildOptions(), argsa);
        StringBuilder JSONStringData = new StringBuilder();
        technology.tabula.CommandLineApp commandLineApp = new technology.tabula.CommandLineApp(JSONStringData, cmd);
        commandLineApp.extractTables(cmd);
        // 读取表格数据
        JSONArray objects = JSON.parseArray(JSONStringData.toString());
        JSONObject jsonObject = JSON.parseObject(objects.get(0).toString());
        JSONArray allData = jsonObject.getJSONArray("data");
        List<List<Map<String, String>>> rowData = JSON.parseObject(allData.toJSONString(), List.class);

        String operationNameTitle = rowData.get(0).get(0).get("text");
        System.out.println("工序名标题，正常是‘工程名(作业名)’："+operationNameTitle);
        String operationName = rowData.get(0).get(1).get("text");
        System.out.println("工序名称："+operationName);
        System.out.println(objects);
        return null;

    }
    public static List<MultipartFile> readPDFList() throws ParseException {

        String[] argsa = new String[]{"-f=JSON", "-p=1", "C:\\Users\\25036\\IdeaProjects\\emis\\output\\test(1).pdf", "-l"};
        CommandLineParser parser = new DefaultParser();
        CommandLine cmd = null;
        cmd = parser.parse(CommandLineApp.buildOptions(), argsa);
        StringBuilder JSONStringData = new StringBuilder();
        technology.tabula.CommandLineApp commandLineApp = new technology.tabula.CommandLineApp(JSONStringData, cmd);
        commandLineApp.extractTables(cmd);
        // 读取表格数据
        JSONArray objects = JSON.parseArray(JSONStringData.toString());

        JSONObject jsonObject = JSON.parseObject(objects.get(0).toString());

        JSONArray allData = jsonObject.getJSONArray("data");
        List<List<Map<String, String>>> rowData = JSON.parseObject(allData.toJSONString(), List.class);
        // System.out.println(rowData);
        List<ProductSop> operationDataList = new ArrayList<>();
        // 遍历每行
        rowData.forEach(row -> {
            // 第一列序号
            String sortNo = row.get(0).get("text");
            // 第二列标签号
            String tagNo = row.get(1).get("text");
            // 第三列管理no
            String managerNo = row.get(2).get("text");
            // 第四列工序号
            String operationNo = row.get(3).get("text");
            // 第五列工序名称
            String operationName = row.get(4).get("text");
            // 第六列作成年月日
            String finishedTime = row.get(5).get("text");
            if (!StringUtils.isAnyBlank(sortNo, managerNo, operationName, operationNo, finishedTime) && !sortNo.equals("序号")) {
                // 去除空格换行等
                operationName = operationName.trim().replaceAll("\r", "")
                        .replace(" ", "").replaceAll(" ", "").replaceAll(" +", "").replaceAll("\\s*", "");

                ProductSop productSop = ProductSop.builder()
                        .managerNo(managerNo)
                        .operationNo(Integer.valueOf(operationNo))
                        .operationName(operationName)
                        .finishedTime(DateUtil.parse(finishedTime))
                        .build();
                operationDataList.add(productSop);
                System.out.println(productSop);
            }


        });
        System.out.println(operationDataList.get(15).getOperationName());

        return null;
    }




}
